home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Visual Database / Visual BASIC 5.0 (Ent. Edition) / Vb5ent Extractor.EXE / VB / SAMPLES / COMPTOOL / DATATREE / TREEVIEW.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-09-16  |  16.9 KB  |  465 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.0#0"; "COMDLG32.OCX"
  3. Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#1.0#0"; "COMCTL32.OCX"
  4. Begin VB.Form frmTreeview 
  5.    Caption         =   "Database-bound TreeView"
  6.    ClientHeight    =   5655
  7.    ClientLeft      =   735
  8.    ClientTop       =   2640
  9.    ClientWidth     =   9660
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   5655
  12.    ScaleWidth      =   9660
  13.    Begin VB.ComboBox Combo1 
  14.       Height          =   315
  15.       Left            =   6960
  16.       TabIndex        =   4
  17.       Text            =   "Combo1"
  18.       Top             =   0
  19.       Width           =   2415
  20.    End
  21.    Begin VB.CommandButton cmdLoad 
  22.       Caption         =   "Load"
  23.       Height          =   270
  24.       Left            =   2190
  25.       TabIndex        =   1
  26.       Top             =   4935
  27.       Visible         =   0   'False
  28.       Width           =   1050
  29.    End
  30.    Begin ComctlLib.TabStrip TabStrip1 
  31.       Height          =   30
  32.       Left            =   1305
  33.       TabIndex        =   6
  34.       Top             =   5145
  35.       Width           =   30
  36.       _ExtentX        =   53
  37.       _ExtentY        =   53
  38.       BeginProperty Tabs {0713E432-850A-101B-AFC0-4210102A8DA7} 
  39.          NumTabs         =   1
  40.          BeginProperty Tab1 {0713F341-850A-101B-AFC0-4210102A8DA7} 
  41.             ImageVarType    =   2
  42.          EndProperty
  43.       EndProperty
  44.    End
  45.    Begin ComctlLib.StatusBar sbrDB 
  46.       Align           =   2  'Align Bottom
  47.       Height          =   255
  48.       Left            =   0
  49.       TabIndex        =   5
  50.       Top             =   5400
  51.       Width           =   9660
  52.       _ExtentX        =   17039
  53.       _ExtentY        =   450
  54.       BeginProperty Panels {0713E89E-850A-101B-AFC0-4210102A8DA7} 
  55.          NumPanels       =   1
  56.          BeginProperty Panel1 {0713E89F-850A-101B-AFC0-4210102A8DA7} 
  57.          EndProperty
  58.       EndProperty
  59.    End
  60.    Begin ComctlLib.ProgressBar prgLoad 
  61.       Height          =   210
  62.       Left            =   255
  63.       TabIndex        =   3
  64.       Top             =   375
  65.       Visible         =   0   'False
  66.       Width           =   9090
  67.       _ExtentX        =   16034
  68.       _ExtentY        =   370
  69.    End
  70.    Begin ComctlLib.ListView lvwDB 
  71.       Height          =   4215
  72.       Left            =   3480
  73.       TabIndex        =   2
  74.       Top             =   615
  75.       Width           =   5880
  76.       _ExtentX        =   10372
  77.       _ExtentY        =   7435
  78.       LabelWrap       =   -1  'True
  79.       HideSelection   =   -1  'True
  80.       Icons           =   "imlIcons"
  81.       SmallIcons      =   "imlSmallIcons"
  82.       ForeColor       =   -2147483640
  83.       BackColor       =   -2147483643
  84.       NumItems        =   0
  85.    End
  86.    Begin MSComDlg.CommonDialog dlgDialog 
  87.       Left            =   1605
  88.       Top             =   4830
  89.       _ExtentX        =   847
  90.       _ExtentY        =   847
  91.       FilterIndex     =   474
  92.       FontSize        =   8.01821e-38
  93.    End
  94.    Begin ComctlLib.TreeView tvwDB 
  95.       Height          =   4215
  96.       Left            =   255
  97.       TabIndex        =   0
  98.       Top             =   615
  99.       Width           =   3120
  100.       _ExtentX        =   5503
  101.       _ExtentY        =   7435
  102.       LineStyle       =   1
  103.       PathSeparator   =   "\"
  104.       Style           =   7
  105.       ImageList       =   "imlSmallIcons"
  106.    End
  107.    Begin VB.Label lblI 
  108.       Caption         =   "To load the Biblio.mdb, on the File menu, click Load."
  109.       Height          =   255
  110.       Left            =   2520
  111.       TabIndex        =   7
  112.       Top             =   0
  113.       Width           =   4335
  114.    End
  115.    Begin ComctlLib.ImageList imlIcons 
  116.       Left            =   645
  117.       Top             =   4800
  118.       _ExtentX        =   1005
  119.       _ExtentY        =   1005
  120.       BackColor       =   -2147483643
  121.       ImageWidth      =   32
  122.       ImageHeight     =   32
  123.       MaskColor       =   12632256
  124.       BeginProperty Images {0713E8C2-850A-101B-AFC0-4210102A8DA7} 
  125.          NumListImages   =   1
  126.          BeginProperty ListImage1 {0713E8C3-850A-101B-AFC0-4210102A8DA7} 
  127.             Picture         =   "treeview.frx":0000
  128.             Key             =   "book"
  129.          EndProperty
  130.       EndProperty
  131.    End
  132.    Begin ComctlLib.ImageList imlSmallIcons 
  133.       Left            =   45
  134.       Top             =   4815
  135.       _ExtentX        =   1005
  136.       _ExtentY        =   1005
  137.       BackColor       =   -2147483643
  138.       ImageWidth      =   13
  139.       ImageHeight     =   13
  140.       MaskColor       =   12632256
  141.       BeginProperty Images {0713E8C2-850A-101B-AFC0-4210102A8DA7} 
  142.          NumListImages   =   6
  143.          BeginProperty ListImage1 {0713E8C3-850A-101B-AFC0-4210102A8DA7} 
  144.             Picture         =   "treeview.frx":031A
  145.             Key             =   "closed"
  146.          EndProperty
  147.          BeginProperty ListImage2 {0713E8C3-850A-101B-AFC0-4210102A8DA7} 
  148.             Picture         =   "treeview.frx":048C
  149.             Key             =   "cylinder"
  150.          EndProperty
  151.          BeginProperty ListImage3 {0713E8C3-850A-101B-AFC0-4210102A8DA7} 
  152.             Picture         =   "treeview.frx":05FE
  153.             Key             =   "leaf"
  154.          EndProperty
  155.          BeginProperty ListImage4 {0713E8C3-850A-101B-AFC0-4210102A8DA7} 
  156.             Picture         =   "treeview.frx":0770
  157.             Key             =   "open"
  158.          EndProperty
  159.          BeginProperty ListImage5 {0713E8C3-850A-101B-AFC0-4210102A8DA7} 
  160.             Picture         =   "treeview.frx":08E2
  161.             Key             =   "smlBook"
  162.          EndProperty
  163.          BeginProperty ListImage6 {0713E8C3-850A-101B-AFC0-4210102A8DA7} 
  164.             Picture         =   "treeview.frx":0B94
  165.             Key             =   ""
  166.          EndProperty
  167.       EndProperty
  168.    End
  169.    Begin VB.Menu mnuFile 
  170.       Caption         =   "File"
  171.       Begin VB.Menu mnuLoad 
  172.          Caption         =   "Load"
  173.       End
  174.       Begin VB.Menu mnuExit 
  175.          Caption         =   "Exit"
  176.       End
  177.    End
  178. Attribute VB_Name = "frmTreeview"
  179. Attribute VB_Base = "0{29AE1516-DB43-11CF-AE2E-00AA006EC21C}"
  180. Attribute VB_GlobalNameSpace = False
  181. Attribute VB_Creatable = False
  182. Attribute VB_TemplateDerived = False
  183. Attribute VB_PredeclaredId = True
  184. Attribute VB_Exposed = False
  185. Private mDbBiblio As Database ' Module-level Database variable.
  186. Private mNode As Node ' Module-level variable for Nodes
  187. Private mWksBiblio As Workspace ' Module-level WorkSpace variable.
  188. Private mItem As ListItem ' Module-level ListItem variable.
  189. Private EventFlag As Integer ' To signal which event has occurred.
  190. Private mCurrentIndex As Integer ' Flag to assure this node wasn't already clicked.
  191. Private mStatusBarStyle As Integer ' Switches Statusbar style
  192. Const PUBLISHER = 1 ' For EventFlag, Signals Publisher colmunheader objects.
  193. Const TITLE = 2 ' EventFlag, signals Title in ListView
  194. Private Sub cmdLoad_Click()
  195.     ' Declare variables for the Data Access objects.
  196.     Dim rsPublishers As Recordset
  197.     Dim rsTitles As Recordset
  198.     Dim intIndex ' Variable for index of current node.
  199.     ' Set the Database to the BIBLIO.MDB database.
  200.     ' Set the recordset to the "Authors" table.
  201.     Set rsPublishers = mDbBiblio. _
  202.     OpenRecordset("Publishers", dbOpenDynaset)
  203.     ' Show the Progressbar.
  204.     prgLoad.Visible = True
  205.     ' Set StatusBar style to simple
  206.     sbrDB.Style = sbrSimple
  207.     ' While the record is not the last record, add a ListItem object.
  208.     ' Use the Name field for the ListItem object's text.
  209.     Do Until rsPublishers.EOF
  210.         
  211.         prgLoad.Value = rsPublishers.PercentPosition
  212.         ' Add a Node to the TreeView, and set its properties.
  213.         Set mNode = tvwDB.Nodes.Add(1, tvwChild, rsPublishers!PubID & " ID", CStr(rsPublishers!Name), "closed")
  214.         mNode.Tag = "Publisher" ' Identifies the table.
  215.         ' Set the variable intIndex to the Index property of the
  216.         ' newly created Node. Use this variable to add child
  217.         ' Node objects to the present Node.
  218.         intIndex = mNode.index
  219.         ' While on this record, create a recordset
  220.         ' using a query that finds only titles that have
  221.         ' the same PubID. For each record in the
  222.         ' resulting recordset, add a Node object to the
  223.         ' TreeView control, and set the new Node object
  224.         ' properties with the record's Title, ISBN and
  225.         ' Author fields.
  226.         Set rsTitles = mDbBiblio.OpenRecordset _
  227.         ("select * from Titles Where PubID = " & rsPublishers!PubID)
  228.         Do Until rsTitles.EOF
  229.             Set mNode = tvwDB.Nodes.Add(intIndex, tvwChild)
  230.             mNode.Text = rsTitles!TITLE ' Text.
  231.             mNode.Key = rsTitles!ISBN   ' Unique ID.
  232.             mNode.Tag = "Authors"       ' Table name.
  233.             mNode.Image = "smlBook"     ' Image from ImageList.
  234.             rsTitles.MoveNext ' Move to next record in rsTitles.
  235.         Loop
  236.         rsPublishers.MoveNext   ' Move to next Publishers record.
  237.     Loop
  238.     ' Hide Progressbar
  239.     prgLoad.Visible = False
  240.     ' Set Statusbar style to normal.
  241.     sbrDB.Style = sbrNormal
  242.     ' Sort the Publishers nodes.
  243.     tvwDB.Nodes(1).Sorted = True
  244.     ' Expand top node.
  245.     tvwDB.Nodes(1).Expanded = True
  246.     ' configure statusbar.
  247.     PublishersStatusBar
  248. End Sub
  249. Private Sub PublishersStatusBar()
  250.     Dim rs As Recordset
  251.     Set rs = mDbBiblio.OpenRecordset("Publishers", dbOpenTable)
  252.     sbrDB.Panels(1).Text = "name: " & rs.Name
  253.     sbrDB.Panels(2).Text = "Date Created: " & _
  254.     CStr(rs.DateCreated)
  255.     sbrDB.Panels(3).Text = "Last Updated " & _
  256.     rs.LastUpdated
  257.     sbrDB.Panels(4).Text = "Lockedits: " & rs.LockEdits
  258. End Sub
  259. Private Sub Combo1_Click()
  260.     lvwDB.View = Combo1.ListIndex
  261. End Sub
  262. Private Sub Form_Load()
  263.     ' Configure Combo1 control.
  264.     With Combo1
  265.         .AddItem "Icon View"      '0
  266.         .AddItem "SmallIcon View" '1
  267.         .AddItem "List View"         '2
  268.         .AddItem "Report View"       '3
  269.         .ListIndex = 3
  270.     End With
  271.     ' Configure ListView control.
  272.     lvwDB.View = lvwReport
  273.         
  274.     ' Configure ProgressBar.
  275.     prgLoad.Max = 100
  276.     ' Configure StatusBar control.
  277.     ' Add three panels, and set Autosize for each
  278.     sbrDB.Panels.Add
  279.     sbrDB.Panels.Add
  280.     sbrDB.Panels.Add
  281.     sbrDB.Panels(1).AutoSize = sbrContents
  282.     sbrDB.Panels(2).AutoSize = sbrSpring
  283.     sbrDB.Panels(3).AutoSize = sbrSpring
  284.     sbrDB.Panels(4).AutoSize = sbrContents
  285.     ' Configure cmdDialog in case the Biblio.mdb can't be found.
  286.     With dlgDialog
  287.         .DialogTitle = "Can't Find Biblio.mdb"
  288.         
  289.         .Filter = "(*.MDB)|*.mdb"
  290.     End With
  291.     On Error GoTo errFind
  292.     ' Open the Biblio.MDB and set the object variable to the database.
  293.     Set mDbBiblio = DBEngine.OpenDatabase("BIBLIO.MDB")
  294.     ' Configure TreeView
  295.     tvwDB.Sorted = True
  296.     Set mNode = tvwDB.Nodes.Add()
  297.     'mNode.Text = mWksBiblio.Databases(0).Name
  298.     mNode.Text = "Publishers"
  299.     mNode.Tag = mDbBiblio.Name
  300.     mNode.Image = "closed"
  301.     tvwDB.LabelEdit = False
  302.     ' If the Biblio database can't be found, open the
  303.     ' common dialog control to let the user find it.
  304. errFind:
  305.     If Err = 3024 Then
  306.         dlgDialog.ShowOpen
  307.         Set mDbBiblio = DBEngine.Workspaces(0).OpenDatabase(dlgDialog.Filename)
  308.         Resume Next
  309.     ElseIf Err <> 0 Then ' another error
  310.         MsgBox "Unexpected Error: " & Err.Description
  311.         End
  312.     ElseIf Err = 0 Then
  313.         Exit Sub ' just go on
  314.     End If
  315. End Sub
  316. Private Sub lvwDB_ColumnClick(ByVal ColumnHeader As ColumnHeader)
  317.     lvwDB.SortKey = ColumnHeader.index - 1
  318.     ' Set Sorted to True to sort the list.
  319.     lvwDB.Sorted = True
  320. End Sub
  321. Private Sub lvwDB_ItemClick(ByVal Item As ListItem)
  322.     GetData (Item.Key)
  323. End Sub
  324. Private Sub GetData(ISBN)
  325.     If EventFlag <> TITLE Then
  326.         sbrDB.Panels.Clear
  327.         Dim pnlX As Panel
  328.         Set pnlX = sbrDB.Panels.Add(, "ISBN")
  329.         pnlX.AutoSize = sbrSpring
  330.        Set pnlX = sbrDB.Panels.Add(, "author")
  331.         pnlX.AutoSize = sbrSpring
  332.         Set pnlX = sbrDB.Panels.Add(, "year")
  333.         pnlX.Width = 1000
  334.         Set pnlX = sbrDB.Panels.Add(, "description")
  335.         pnlX.AutoSize = sbrSpring
  336.     End If
  337.     Dim rsTitles As Recordset
  338.     ' Set object variable to "Titles" recordset
  339.     Set rsTitles = mDbBiblio.OpenRecordset("Titles", dbOpenDynaset)
  340.     ' Find the ISBN number in Titles table and populate
  341.     ' StatusBar panels with info.
  342.     Dim strQuery As String
  343.     strQuery = "ISBN = " & "'" & ISBN & "'"
  344.     rsTitles.FindFirst strQuery
  345.     sbrDB.Panels("author").Text = GetAuthor(ISBN)
  346.     sbrDB.Panels("ISBN").Text = rsTitles!ISBN
  347.     If Not IsNull(rsTitles![Year Published]) Then
  348.         sbrDB.Panels("year").Text = rsTitles![Year Published]
  349.     Else
  350.         sbrDB.Panels("year").Text = "n/a"
  351.     End If
  352.     If Not IsNull(rsTitles!Description) Then
  353.         sbrDB.Panels("description").Text = rsTitles!Description
  354.     Else
  355.         sbrDB.Panels("description").Text = "n/a"
  356.     End If
  357.         
  358.     ' Set EventFlag so Panels don't have to be recreated.
  359.     EventFlag = TITLE
  360. End Sub
  361. Private Sub mnuExit_Click()
  362.     End
  363. End Sub
  364. Private Sub mnuLoad_Click()
  365.     Static Loaded As Boolean
  366.     If Loaded = True Then
  367.         Exit Sub
  368.     Else
  369.         cmdLoad_Click
  370.         Loaded = Abs(Loaded - 1)
  371.         mnuLoad.Enabled = False
  372.         
  373.     End If
  374. End Sub
  375. Private Sub tvwDB_Collapse(ByVal Node As Node)
  376.     If Node.Tag = "Publisher" Or Node.index = 1 _
  377.     Then Node.Image = "closed"
  378. End Sub
  379. Private Sub tvwDB_Expand(ByVal Node As Node)
  380.     If Node.Tag = "Publisher" Or Node.index = 1 Then
  381.         Node.Image = "open"
  382.         Node.Sorted = True
  383.     End If
  384. End Sub
  385. Private Sub MakeColumns()
  386.     ' Clear the ColumnHeaders collection.
  387.     lvwDB.ColumnHeaders.Clear
  388.     ' Add four ColumnHeaders.
  389.     lvwDB.ColumnHeaders.Add , , "Title", 2000
  390.     lvwDB.ColumnHeaders.Add , , "Author"
  391.     lvwDB.ColumnHeaders.Add , , "Year", 350
  392.     lvwDB.ColumnHeaders.Add , , "ISBN"
  393.     ' Set the EventFlag variable so this doesn't get done again and again.
  394.     EventFlag = PUBLISHER
  395. End Sub
  396. Private Sub GetTitles(PubID)
  397.     ' Show Progress bar
  398.     prgLoad.Visible = True
  399.     ' Clear the old titles
  400.     lvwDB.ListItems.Clear
  401.     ' Declare object variable of type Recordset
  402.     Dim rsTitles As Recordset
  403.     ' While on this record, create a recordset using a query
  404.     ' that finds only titles that have the same PubID.
  405.     ' For each record in this recordset, add a ListItem object
  406.     ' to the ListView control, and set the new object's properties
  407.     ' with the record's Title, ISBN and Author fields.
  408.     Set rsTitles = mDbBiblio.OpenRecordset _
  409.     ("select * from Titles where PubID = " & PubID)
  410.     Do Until rsTitles.EOF
  411.             prgLoad.Value = rsTitles.PercentPosition
  412.             Set mItem = lvwDB.ListItems.Add _
  413.             (, CStr(rsTitles!ISBN), rsTitles!TITLE, "book", "smlBook")
  414.             
  415.             ' Use a function to get the author and set
  416.             ' the SubItems(1) property.
  417.             mItem.SubItems(1) = GetAuthor(rsTitles!ISBN)
  418.             If Not IsNull(rsTitles![Year Published]) Then
  419.                 mItem.SubItems(2) = rsTitles![Year Published]
  420.             End If
  421.             mItem.SubItems(3) = rsTitles!ISBN
  422.             rsTitles.MoveNext
  423.     Loop
  424.     prgLoad.Visible = False
  425.     mCurrentIndex = PubID
  426. End Sub
  427. Private Function GetAuthor(ISBN)
  428.     ' Returns the name of an author from Titles table.
  429.     ' Open two recordsets. Search "Title Author" for the ISBN. Then use the
  430.     ' Au_ID field and search for the same value in "Authors" table. Return Author name.
  431.     Dim rsTitlesAuthor As Recordset
  432.     Dim rsAuthors As Recordset
  433.     Set rsTitlesAuthor = mDbBiblio.OpenRecordset("Title Author", dbOpenDynaset)
  434.     Set rsAuthors = mDbBiblio.OpenRecordset("Authors", dbOpenDynaset)
  435.     Dim strQuery As String
  436.     strQuery = "ISBN = " & "'" & ISBN & "'"
  437.     rsTitlesAuthor.FindFirst strQuery
  438.     If rsTitlesAuthor.NoMatch Then
  439.         GetAuthor = "n/a"
  440.         Exit Function
  441.     Else
  442.         strQuery = "Au_ID = " & rsTitlesAuthor!AU_ID
  443.         rsAuthors.FindFirst strQuery
  444.     End If
  445.     GetAuthor = rsAuthors!Author
  446. End Function
  447. Private Sub tvwDB_NodeClick(ByVal Node As Node)
  448.     ' Check the Tag for "Publisher" and EventFlag
  449.     ' variable to see if the ColumnHeaders
  450.     ' have already been created. If not, then
  451.     ' invoke the MakeColumns procedure.
  452.     If Node.Tag = "Publisher" And EventFlag <> _
  453.     PUBLISHER Then MakeColumns
  454.     ' If the Tag is "Publisher" and the mItemCurrentIndex
  455.     ' index isn't the same as the Node.key, then
  456.     ' incoke the GetTitles procedure.
  457.     If Node.Tag = "Publisher" And mCurrentIndex <> Val(Node.Key) _
  458.     Then GetTitles Val(Node.Key)
  459.     If Node.Tag = "Publisher" Then
  460.         sbrDB.Panels(1).Text = Node.Text
  461.         sbrDB.Panels(2).Text = Node.Children & " titles"
  462.         Node.Sorted = True
  463.     End If
  464. End Sub
  465.